#include <gtest/gtest.h>
#include "../mqserver/consumer.hpp"

using namespace std;
using namespace ns_rabbitmq;

ConsumerManangerPtr cmp;
class ConsumerTets : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        cmp = std::make_shared<ConsumerMananger>();
        cmp->InitQueueConsumer("queue1");
    }
    virtual void TearDown()
    {
        // bptr->Clear();
        std::cout << "完成清理工作" << std::endl;
    }
};

void cb(const std::string &tag, BasicProperties *bp, const std::string &body)
{
    std::cout << tag << "消费了:" <<body << std::endl;
}

TEST(consumer, insert_test)
{
    cmp->Create("consumer1", "queue1", true, cb);
    cmp->Create("consumer2", "queue1", false, cb);
    cmp->Create("consumer3", "queue1", false, cb);
    ASSERT_EQ(cmp->IsConsumerExists("consumer1", "queue1"), true);
    ASSERT_EQ(cmp->IsConsumerExists("consumer2", "queue1"), true);
    ASSERT_EQ(cmp->IsConsumerExists("consumer3", "queue1"), true);
    ASSERT_EQ(cmp->IsConsumerExists("consumer4", "queue1"), false);
}

TEST(consumer, ack_test)
{
    cmp->Remove("consumer1", "queue1");
    ASSERT_EQ(cmp->IsConsumerExists("consumer1", "queue1"), false);
}

TEST(consumer, choose)
{
    ASSERT_EQ(cmp->IsQueueEmpty("queue1"), false);
    ConsumerPtr cp = cmp->Chose("queue1");
    ASSERT_NE(cp.get(), nullptr);
    ASSERT_EQ(cp->_tag, "consumer2");

    ConsumerPtr cp1 = cmp->Chose("queue1");
    ASSERT_EQ(cp1->_tag, "consumer3");

    ConsumerPtr cp2 = cmp->Chose("queue1");
    ASSERT_EQ(cp2->_tag, "consumer2");
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ConsumerTets);
    RUN_ALL_TESTS();
    return 0;
}